﻿// LaserPlaneParameters
// Container for LaserPlane parameters
// 
// FLMScan - Featured by FabLab Munich

using System;
using System.IO;
using System.Xml;
using System.Globalization;
using log4net;

namespace FLMScan.Parameters
{
    class LaserPlaneParameters
    {
        private ILog logger;

        private const double DefaultParameterA = 0.0;
        private const double DefaultParameterB = 0.0;
        private const double DefaultParameterC = -1.0;
        private const double DefaultParameterD = -10.0;

        private double parameterA;
        private double parameterB;
        private double parameterC;
        private double parameterD;

        private const String LaserPlaneParameterAParameter = "LaserPlaneParameterA";
        private const String LaserPlaneParameterBParameter = "LaserPlaneParameterB";
        private const String LaserPlaneParameterCParameter = "LaserPlaneParameterC";
        private const String LaserPlaneParameterDParameter = "LaserPlaneParameterD";

        public LaserPlaneParameters(double a, double b, double c, double d)
        {
            ParameterA = a;
            ParameterB = b;
            ParameterC = c;
            ParameterD = d;
        }

        public LaserPlaneParameters(string configurationFilePath)
        {
            logger = LogManager.GetLogger(typeof(LaserPlaneParameters));
            Initialize(configurationFilePath);            
        }

        public void Serialize(XmlTextWriter textWriter)
        {
            FLMScanParameters.SerializeParameter(textWriter, LaserPlaneParameterAParameter, ParameterA.ToString());
            FLMScanParameters.SerializeParameter(textWriter, LaserPlaneParameterBParameter, ParameterB.ToString());
            FLMScanParameters.SerializeParameter(textWriter, LaserPlaneParameterCParameter, ParameterC.ToString());
            FLMScanParameters.SerializeParameter(textWriter, LaserPlaneParameterDParameter, ParameterD.ToString());
        }

        private void Initialize(string configurationFilePath)
        {
            SetDefaultValues();

            try
            {
                TextReader reader = new StreamReader(configurationFilePath);
                GetConfigValues(reader);
            }
            catch (FileNotFoundException)
            {

            }
        }

        private void SetDefaultValues()
        {
            parameterA = DefaultParameterA;
            parameterB = DefaultParameterB;
            parameterC = DefaultParameterC;
            parameterD = DefaultParameterD;
        }        

        private void GetConfigValues(TextReader reader)
        {
            XmlTextReader xmlReader = new XmlTextReader(reader);
            while (xmlReader.Read())
            {
                if (xmlReader.NodeType == XmlNodeType.Element &&
                    xmlReader.Name.Equals("add") &&
                    xmlReader.MoveToNextAttribute())
                {
                    try
                    {
                        switch (xmlReader.Name)
                        {
                            case LaserPlaneParameterAParameter:
                                string value = xmlReader.Value;
                                string ensureDecimalPoints = value.Replace('.', ',');
                                ParameterA = double.Parse(ensureDecimalPoints, NumberStyles.Float);
                                logger.DebugFormat(FLMScanParameters.LogSettingParameterToValue, LaserPlaneParameterAParameter, ParameterA);
                                break;
                            case LaserPlaneParameterBParameter:
                                value = xmlReader.Value;
                                ensureDecimalPoints = value.Replace('.', ',');
                                ParameterB = double.Parse(ensureDecimalPoints, NumberStyles.Float);
                                logger.DebugFormat(FLMScanParameters.LogSettingParameterToValue, LaserPlaneParameterBParameter, ParameterB);
                                break;
                            case LaserPlaneParameterCParameter:
                                value = xmlReader.Value;
                                ensureDecimalPoints = value.Replace('.', ',');
                                ParameterC = double.Parse(ensureDecimalPoints, NumberStyles.Float);
                                logger.DebugFormat(FLMScanParameters.LogSettingParameterToValue, LaserPlaneParameterCParameter, ParameterC);
                                break;
                            case LaserPlaneParameterDParameter:
                                value = xmlReader.Value;
                                ensureDecimalPoints = value.Replace('.', ',');
                                ParameterD = double.Parse(ensureDecimalPoints, NumberStyles.Float);
                                logger.DebugFormat(FLMScanParameters.LogSettingParameterToValue, LaserPlaneParameterDParameter, ParameterD);
                                break;
                        }
                    }
                    catch (System.Exception e)
                    {
                        System.Diagnostics.Debug.Assert(false, e.ToString());
                        logger.Error(FLMScanParameters.LogExceptionReadingParametersFromConfigFile, e);
                    }
                }
            }

            xmlReader.Close();
        }

        public double ParameterA
        {
            get { return parameterA; }
            set { parameterA = value; }
        }

        public double ParameterB
        {
            get { return parameterB; }
            set { parameterB = value; }
        }

        public double ParameterC
        {
            get { return parameterC; }
            set { parameterC = value; }
        }

        public double ParameterD
        {
            get { return parameterD; }
            set { parameterD = value; }
        }

        
    }
}
